
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using HomeDMSObjectModel;
using NHibernate;
using HomeDms.Properties;



namespace HomeDms
{
    public partial class DocumentListForm : Form
    {
        private DBHelper _dbhelper;
        
        public DocumentListForm()
        {
            InitializeComponent();
        }

        private void DocumentListForm_Shown(object sender, EventArgs e)
        {
            if (!Settings.Default.SkipStartScreen)
                using (ActionsForm af = new ActionsForm())
                {
                    af.DBHelperProp = _dbhelper;
                    af.ShowDialog();
                }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (AboutForm af = new AboutForm())
                af.ShowDialog();
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (PreferancesForm pf = new PreferancesForm())
                pf.ShowDialog();
        }

        private void DocumentListForm_Load(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void RefreshData()
        {
            FileInfo fi = new FileInfo(Application.ExecutablePath);
            _dbhelper = new DBHelper(fi.Directory.FullName);
            //It's simple query example...
            ISession session = _dbhelper.OpenSession();
            ICriteria criteria = session.CreateCriteria(typeof(Document));
            documentsBindingSource.DataSource = criteria.List<Document>();
            documentsBindingSource.ResetBindings(false);
            session.Close();
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenDocumentForm(false);
        }

        private void changeCurrentUserToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void backupToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void restoreDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenDocumentForm(true);
        }

        private void deleteDocumentToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void findToolStripButton_Click(object sender, EventArgs e)
        {
            if (sender is ToolStripMenuItem)
                findToolStripButton.Checked = findDocumentToolStripMenuItem.Checked;
            else
                findDocumentToolStripMenuItem.Checked = findToolStripButton.Checked;
            splitContainer.Panel1Collapsed = !findToolStripButton.Checked;
        }

        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            OpenDocumentForm(true);
        }

        private void OpenDocumentForm(bool forAdd)
        {
            DocumentForm dForm = new DocumentForm();
            dForm.DocumentsUpdated += new EventHandler(dForm_DocumentsUpdated);
            dForm.DBHelperProp = _dbhelper;
            if (!forAdd)
                dForm.Document = documentsBindingSource.Current as Document;
            DialogResult res = dForm.ShowDialog(this);
            if (res == DialogResult.OK)
            {
                //documentsBindingSource.Add(dForm.Document);
            }
        }

        void dForm_DocumentsUpdated(object sender, EventArgs e)
        {
            RefreshData();
            documentsBindingSource.ResetBindings(false);
        }

    }
}
